package com.yogee.topseller.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.gson.Gson;
import com.yogee.core.utils.AppManager;
import com.yogee.core.utils.NetUtil;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.MainActivity;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.base.HttpActivity;
import com.yogee.topseller.activity.login.view.impl.RegisterLabelActivity;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.Config;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.LogUtils;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/3/18.
 */

public class HyqxActivity1 extends HttpActivity {

    @BindView(R.id.add_card_titlelayout)
    TitleLayout titlelayout;
    @BindView(R.id.wb)
    WebView wb;
    @BindView(R.id.myProgressBar)
    ProgressBar bar;
    private String fxjg = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hyqx;
    }

    @Override
    protected void initView() {
        titlelayout.setActivity(this);
        titlelayout.setTitle("查询");
        fxjg = getIntent().getStringExtra("fxjg");
        wb.getSettings().setDomStorageEnabled(true);
        wb.getSettings().setDatabaseEnabled(true);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setDefaultTextEncodingName("GBK");
        wb.getSettings().setAllowFileAccessFromFileURLs(false);
        wb.getSettings().setAllowUniversalAccessFromFileURLs(false);



        wb.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    bar.setVisibility(View.GONE);
//                    if(pd!=null)
//                        pd.dismiss();
                } else {
                    if (View.GONE == bar.getVisibility()) {
                        bar.setVisibility(View.VISIBLE);
                    }
                    bar.setProgress(newProgress);
//                    if(pd!=null&&!pd.isShowing()){
//                        pd.show();
//                    }
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        wb.addJavascriptInterface(this, "nativeMethod");
        wb.loadUrl("https://m.tianyancha.com/company/"+fxjg);
        wb.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

//                LogUtils.e("加载成功1111","id:"+url);
//                try{
//                    wb.loadUrl("javascript:document.getElementById('banner_mobile').style.display='none'");
//                    wb.loadUrl("javascript:document.getElementsByClassName('wapHeader')[0].style.display='none'");
//                }catch (Exception e){
//
//                }

//                handler.sendEmptyMessage(2);//处理主线程方法
            }
        });

    }



    private static final int SUCCESS = 0;
    protected static final int ERROR = 1;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SUCCESS:
                    String id = (String) msg.obj;
                    LogUtils.e("加载成功","id:"+id);
                    wb.loadUrl("https://m.tianyancha.com/company/"+id);
//                    wb.loadUrl("http://www.baidu.com");

                    wb.setWebViewClient(new WebViewClient(){

                        @Override
                        public void onPageFinished(WebView view, String url) {
                            super.onPageFinished(view, url);
                            LogUtils.e("加载成功1111","id:");
                            try{
                                wb.loadUrl("javascript:document.getElementById('banner_mobile').style.display='none'");
                                wb.loadUrl("javascript:document.getElementsByClassName('wapHeader')[0].style.display='none'");
                            }catch (Exception e){

                            }
                            handler.sendEmptyMessage(2);//处理主线程方法
                        }
                    });
                    break;
                case ERROR:
                    if(pd!=null)
                        pd.dismiss();
                    ToastUtils.showToast(HyqxActivity1.this,"加载失败");
                    break;
                case 2:
                    LogUtils.e("test","test23");
                    if(pd!=null)
                        pd.dismiss();
                    break;
                default:
                    break;
            }
        }

    };
}
